using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Td.Additional.Log
{
    /// <summary>
    /// Class to fire events for each message.
    /// </summary>
    public class NotifyLog : ILog, INotifyLog
    {
        #region Properties

        /// <summary>
        /// Gets or sets the log level.
        /// </summary>
        /// <value>The log level.</value>
        public LogLevel LogLevel { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [log full qualified method name].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [log full qualified method name]; otherwise, <c>false</c>.
        /// </value>
        public bool LogFullQualifiedMethodName { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="NotifyLog"/> class.
        /// </summary>
        public NotifyLog()
            : base()
        {
            LogLevel = LogLevel.Info;
            LogFullQualifiedMethodName = false;
        }

        #endregion

        #region Private helpers

        /// <summary>
        /// Writes the log entry.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <param name="message">The message.</param>
        private void WriteLogEntry(LogLevel level, String message)
        {
            if (LogLevel == LogLevel.None || level > LogLevel)
                return;
            StackTrace stackTrace = new StackTrace();
            System.Reflection.MethodBase methodBase = null;
            int stackIndex = 2;

            while (stackTrace.FrameCount > stackIndex && typeof(ILog).IsAssignableFrom((methodBase = stackTrace.GetFrame(stackIndex).GetMethod()).ReflectedType))
                stackIndex++;

            string callingMethod = LogFullQualifiedMethodName ? String.Format("{0}.{1}", methodBase.ReflectedType.FullName, methodBase.Name) : methodBase.Name;

            LogEntry logEntry = new LogEntry();
            logEntry.TimeStamp = DateTime.Now;
            logEntry.Level = level;
            logEntry.Source = callingMethod;
            logEntry.Message = message;
            OnNewEntry(logEntry);
        }

        #endregion

        #region ILog members

        /// <summary>
        /// New debug message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Debug(String message)
        {
            WriteLogEntry(LogLevel.Debug, message);
        }

        /// <summary>
        /// New debug message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Debug(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Debug, String.Format(message, args));
        }

        /// <summary>
        /// New information message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Info(String message)
        {
            WriteLogEntry(LogLevel.Info, message);
        }

        /// <summary>
        /// New information message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Info(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Info, String.Format(message, args));
        }

        /// <summary>
        /// New warning message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Warn(String message)
        {
            WriteLogEntry(LogLevel.Warn, message);
        }

        /// <summary>
        /// New warning message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Warn(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Warn, String.Format(message, args));
        }

        /// <summary>
        /// New error message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Error(String message)
        {
            WriteLogEntry(LogLevel.Error, message);
        }

        /// <summary>
        /// New error message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Error(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Error, String.Format(message, args));
        }

        /// <summary>
        /// New critical message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Critical(String message)
        {
            WriteLogEntry(LogLevel.Critical, message);
        }

        /// <summary>
        /// New critical message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Critical(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Critical, String.Format(message, args));
        }

        /// <summary>
        /// New fatal message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Fatal(String message)
        {
            WriteLogEntry(LogLevel.Fatal, message);
        }

        /// <summary>
        /// New fatal message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Fatal(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Fatal, String.Format(message, args));
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when [new entry].
        /// </summary>
        public event EventHandler<EventArgs<LogEntry>> NewEntry;

        /// <summary>
        /// Called when [new entry].
        /// </summary>
        /// <param name="entry">The entry.</param>
        protected void OnNewEntry(LogEntry entry)
        {
            var tempHandler = NewEntry;
            if (tempHandler != null)
                tempHandler(this, new EventArgs<LogEntry>(entry));
        }

        #endregion
    }
}